package LoadAndSaveImage;

import gui.Login;

import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.FileDialog;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.*;

import javax.swing.ImageIcon;
import javax.imageio.*;

import java.io.*;

public class ImagesLoading /* implements ActionListener */
{
	static int Size_x, Size_y;
	JLabel Label1 = new JLabel("Choose your image");
	JButton Button1 = new JButton("select");
	JButton Button2 = new JButton("save");
	Image Image1;

	public ImagesLoading(int width, int height) {
		Size_x = width;
		Size_y = height;
	}

	public Image getImage() {
		return Image1;
	}

	public boolean ImageSave(FileDialog fd, String filename) {
		if (fd.getFile() != null) {
			try {
				BufferedImage originalImage = ImageIO.read(new File(fd
						.getDirectory() + fd.getFile()));
				ImageIO.write(originalImage, "jpg", new File(Login.data_dir
						+ filename));
				return true;
			} catch (IOException e) {
				System.out.println(e.getMessage());
				return false;
			}
		} else
			return true;
	}

	public boolean ImageSelect(FileDialog fd) {
		fd.setFile("*.jpg;*.jpeg");
		fd.pack();
		fd.setVisible(true);
		if (fd.getFile() == null) {
			return false;
		} else {
			String d = (fd.getDirectory() + fd.getFile());
			Toolkit toolkit = Toolkit.getDefaultToolkit();
			Image1 = toolkit.getImage(d);
			ImageIcon icon = new ImageIcon(Image1);
			int height = icon.getIconHeight();
			int width = icon.getIconWidth();
			double ratio = 1;
			if (height > width && height > Size_y)
				ratio = (new Double(Size_y)).doubleValue() / height;
			else if (height < width && width > Size_x)
				ratio = (new Double(Size_x)).doubleValue() / width;
			height *= ratio;
			width *= ratio;
			Image1 = Image1
					.getScaledInstance(width, height, Image.SCALE_SMOOTH);
			return true;
		}
	}

}